package com.party.web.web.controller.analysis;

import com.google.common.collect.Maps;
import com.party.common.paging.Page;
import com.party.common.utils.LangUtils;
import com.party.common.utils.StringUtils;
import com.party.core.model.analysis.StatisticsEvent;
import com.party.core.model.member.MemberGroup;
import com.party.core.service.analysis.IStatisticsEventService;
import com.party.core.service.member.IMemberGroupService;
import com.party.web.utils.RealmUtils;
import com.party.web.web.dto.input.common.CommonInput;
import com.party.web.web.security.CurrentUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName StatisticalEventController统计
 * @Description 行知公社账号登录APP统计控制器
 * @Author shenliuhai
 * @Date 2020/1/6 18:20
 **/
@Controller
@RequestMapping(value = "analysis/event")
public class StatisticalEventController {

    @Autowired
    private IStatisticsEventService iStatisticsEventService;

    @Autowired
    private IMemberGroupService memberGroupService;


    @RequestMapping(value = "/list")
    public ModelAndView list(Page page,StatisticsEvent se, CommonInput commonInput) {

        page.setLimit(10);
        if (null == se.getType()) {
            se.setType(StatisticsEvent.TYPE_MOBILE);
        }

        ModelAndView mv = new ModelAndView("analysis/statisticalEventCountList");
        if (commonInput.getTimeType() == null&&
                (StringUtils.isBlank(commonInput.getCreateEnd())||StringUtils.isBlank(commonInput.getCreateStart()))
        ){
            commonInput.setTimeType(2);
        }
        CommonInput.transfromDate(commonInput);
        Map<String, Object> params = Maps.newHashMap();
        params.put("startDate", commonInput.getCreateStart() + " 00:00:00");
        params.put("endDate", commonInput.getCreateEnd() + " 23:59:59");
        if (StringUtils.isNotEmpty(commonInput.getPartnerName())) {
            params.put("partnerName",commonInput.getPartnerName());
        }

        CurrentUser currentUser = RealmUtils.getNewCurrentUser();

        //获取所有子张号
        List<MemberGroup> childList = memberGroupService.getAllChildWithMy(currentUser.getId());
        List<String> orgIds = LangUtils.transform(childList,(input) ->{
            return input.getId();
        });
        params.put("orgIds",orgIds);

        if (StringUtils.isNotEmpty(commonInput.getCreateStart())) {
            commonInput.setCreateStart(commonInput.getCreateStart().substring(0, 10));
        }
        if (StringUtils.isNotEmpty(commonInput.getCreateEnd())) {
            commonInput.setCreateEnd(commonInput.getCreateEnd().substring(0, 10));
        }
        List<StatisticsEvent> eventList = iStatisticsEventService.listPageByOrgIds(se,params,page);

        mv.addObject("list", eventList);
        mv.addObject("input", commonInput);
        mv.addObject("page",page);
        mv.addObject("se",se);
        return mv;
    }
}
